<template>
	<view>
		<view class="live-top-box">
			<view class="home" @click="goHome">首页</view>
			<view class="search-box">
				<u-search placeholder="请输入直播标题" v-model="keyword"
				 @custom="searchLive"  @search="searchLive"></u-search>
			</view>
		</view>
		<view class="banner">
			<u-swiper :list="swiperList" height="300"></u-swiper>
			<!-- <image :src="baseUrl+live_banner" mode="aspectFill"></image> -->
		</view>
		<view class="menu">
			<view class="item" v-for="item in menu"  @click="navTo('/pages/live/roomList/list?cid='+item.id)">
				<image :src="baseUrl+item.image" mode="aspectFill"></image>
				<view class="txt">{{item.name}}</view>
			</view>
		</view>
		<view class="tabs">
			<u-tabs :list="tabList" :is-scroll="false" active-color="#e02222"
			 :current="tabCurrent" @change="change"></u-tabs>
		</view>
		<view class="live-list">
			<view class="item" v-for="(item,index) in roomList" :key="index"
			 @click="goLive(item)">
				<view class="status" v-if="item.live_status == 0">
					<image src="/static/public/images/zhz.png"></image>
					<text>{{item.start_time}}开播</text>
				</view>
				<view class="status" v-if="item.live_status == 1" style="padding-right: 20rpx;">
					<image src="../static/zhibo.gif" style="width: 60rpx;height: 60rpx;"></image>
					<text style="font-size: 28rpx;position: relative;top: 4rpx;">直播中</text>
				</view>
				<view class="status" v-if="item.live_status == 2">
					<image src="/static/public/images/hf.png"></image>
					<text>精彩回放</text>
				</view>
				<view class="views" v-if="item.live_status == 2">
					{{item.total_view_num}}<text>观看</text>
				</view>
				<image :src="baseUrl + item.cover" mode="aspectFill"></image>
				<view style="margin-top: 10rpx;">
					<image :src="baseUrl + item.avatar" class="avatar">
					<span style="font-size: 14px;" class="u-line-1">{{item.anchor}}</span>
				</view>
			</view>
		</view>
<!-- 		<view class="scroll-list">
			<view class="room-list-content">
				<room-item
					v-for="item in roomList"
					:key="item.id"
					:info="item"
				/>
			</view>
		</view> -->
		<view v-if="emptyVisible" class="room-list-empty">
			暂无直播
		</view>
	</view>
</template>

<script>
	import RoomItem from './roomItem.vue';
	export default {
		components: {
			RoomItem,
		},
		data() {
			return {
				baseUrl: this.config.baseUrl,
				triggered: false,
				fetching: false,
				roomList: [],
				page:1,
				limit:10,
				keyword:'',
				tabList: [{
					name: '全部'
				}, {
					name: '直播中'
				}, {
					name: '预告'
				}, {
					name: '精彩回放'
				}],
				tabCurrent: 0,
				live_banner:'',
				menu:[],
				swiperList:[],
				applet_id:uni.getStorageSync("applet_id"),
			};
		},
		
		onLoad() {
			this.getLiveSet()
			// 获取直播间列表
			this.fetchRoomList();
		},

		onReachBottom() {
			this.page++
			this.triggered = false;
			this.fetchRoomList();
		},
		computed: {
			emptyVisible() {
				return this.roomList.length === 0;
			},
		},
		
		methods: {
			goHome(){
				uni.switchTab({
					url:'/pages/shop/index/index'
				})
			},
			change(index) {
				this.tabCurrent = index;
				this.roomList = []
				this.page = 1
				this.fetchRoomList()
			},
			searchLive(){
				this.roomList = []
				this.page = 1
				this.fetchRoomList()
			},
			getLiveSet(){
				this.$u.get('live/api.index/set').then(res => {
					this.live_banner = res.data.live_banner;
					this.live_banner.forEach(item => {
						this.swiperList.push({image:this.baseUrl+item})
					})
					this.menu = res.data.menu;
				})
			},
			fetchRoomList() {
				uni.showLoading({
					title: '直播间列表加载中'
				});
				this.$u.get('live/api.index/roomList',{
					page:this.page,
					limit:this.limit,
					type:this.tabCurrent,
					keyword:this.keyword
				}).then(res => {
					this.roomList = this.roomList.concat(res.data);
					this.triggered = false;
				}).catch((err) => {
					this.triggered = false;
					uni.hideLoading();
				})
	
			},
			goLive(item){
				if(item.live_status == 2){
					this.app.goPage('/pages/live/record/index?liveId='+item.id+'&liveType='+item.live_type+'&applet_id='+this.applet_id,'web')
				}else{
					this.app.goPage('/pages/live/room/room?liveId='+item.id+'&liveType='+item.live_type+'&applet_id='+this.applet_id,'web')
				}
			}
		}
	}
</script>

<style lang="scss">
	@import './roomList.scss';
	page{
		background-color: #FFFFFF;
	}
	.live-top-box{
		display: flex;
		padding: 20rpx;
		.home{
			width: 70rpx;
			margin-right: 20rpx;
			font-size: 30rpx;
			padding-top: 10rpx;
		}
		.search-box{
			width: 600rpx;
		}
	}
	.banner{
		margin: 20rpx;
		image{
			width: 100%;
			height: 300rpx;
			border-radius: 20rpx;
		}
	}
	.menu{
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx 0;
		.item{
			width: 20%;
			text-align: center;
			margin-bottom: 10px;
			image{
				width: 85rpx;
				height: 85rpx;
			}
			.txt{
				font-size: 26rpx;
				margin-top: 6rpx;
			}
		}
	}
	.tabs{
		margin: 20rpx 0;
		font-size: 28rpx;
	}
	.live-list{
		display: flex;
		flex-wrap: wrap;
		margin: 20rpx;
		.item{
			width: 48%;
			margin-right: 4%;
			position: relative;
			margin-bottom: 30rpx;
			&:nth-child(2n){
				margin-right:0;
			}
			image{
				width: 100%;
				height:340rpx;
				border-radius: 10rpx;
			}
			.avatar{
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				margin-right: 10rpx;
			}
			.status{
				position: absolute;
				top: 10rpx;
				left: 0rpx;
				z-index: 9;
				background-color: rgba(0, 0, 0, 0.5);
				padding: 6rpx 10rpx;
				border-radius: 50rpx;
				transform:scale(0.85);
				image{
					width: 30rpx;
					height: 30rpx;
				}
				text{
					font-size: 24rpx;
					color: #FFFFFF;
					margin-left: 6rpx;
				}
			}
			.views{
				position: absolute;
				top: 16rpx;
				right: 0rpx;
				z-index: 9;
				background-color: rgba(255, 255, 255, 1);
				padding: 2rpx 10rpx;
				border-radius: 50rpx;
				transform:scale(0.8);
				font-size: 24rpx;
				text{
					display: inline-block;
					transform:scale(0.9);
					color: #999;
					margin-left: 10rpx;
				}
			}
		}
	}
</style>
